package edu.whu.webgis.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import edu.whu.webgis.entity.Collection;
import edu.whu.webgis.entity.Posting;
import edu.whu.webgis.entity.VPosting;
import edu.whu.webgis.entity.VSimplePosting;
import edu.whu.webgis.mapper.CollectionMapper;
import edu.whu.webgis.mapper.PostingMapper;
import edu.whu.webgis.mapper.UserMapper;
import edu.whu.webgis.service.IPostingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class PostingServiceImpl implements IPostingService {

    @Autowired
    PostingMapper dao;

    @Autowired
    CollectionMapper collectionMapper;

    @Override
    public void publish(VPosting posting) {
        Posting post = new Posting(posting);
        dao.insert(post);
    }

    public void plusCount(Integer pid){
        dao.plusLike(pid);
    }

    @Override
    public List<VSimplePosting> getPostingByUid(String uid) {
        LambdaQueryWrapper<Posting> qw = new LambdaQueryWrapper<>();
        qw.eq(Posting::getUid,uid);
        List<Posting> postings = dao.selectList(qw);
        List<VSimplePosting> vPostings = new ArrayList<>(postings.size());
        for (Posting posting: postings){
            vPostings.add(new VSimplePosting(posting));
        }
        return vPostings;
    }

    @Override
    public List<VSimplePosting> getCollections(String uid) {
        LambdaQueryWrapper<Collection> qw = new LambdaQueryWrapper<>();
        qw.eq(Collection::getUid,uid);
        List<Collection> collections = collectionMapper.selectList(qw);
        List<VSimplePosting> vPostings = new ArrayList<>(collections.size());
        for (Collection collection: collections){
            vPostings.add(new VSimplePosting(dao.selectById(collection.getPid())));
        }
        return vPostings;
    }

    @Override
    public long getPageCount(Integer size, String keyword) {
        LambdaQueryWrapper<Posting> qw = new LambdaQueryWrapper<>();
        qw.like(Posting::getTitle,keyword);
        qw.or();
        qw.like(Posting::getContent,keyword);
        Page<Posting> page = new Page<>(1,size);
        dao.selectPage(page,qw);
        return page.getPages();
    }

    @Override
    public List<VSimplePosting> selectPage(Integer size, Integer pos, String keyword) {
        LambdaQueryWrapper<Posting> qw = new LambdaQueryWrapper<>();
        qw.like(Posting::getTitle,keyword);
        qw.or();
        qw.like(Posting::getContent,keyword);
        Page<Posting> page = new Page<>(pos,size);
        dao.selectPage(page,qw);
        List<VSimplePosting> postings = new ArrayList<>(size);
        for(Posting record: page.getRecords()){
            postings.add(new VSimplePosting(record));
        }
        return postings;
    }

    @Override
    public VPosting getDetail(Integer id) {
        return new VPosting(dao.selectById(id));
    }

    @Override
    public List<VSimplePosting> selectByCondition(String keyword) {
        LambdaQueryWrapper<Posting> qw = new LambdaQueryWrapper<>();
        qw.like(Posting::getTitle,keyword);
        qw.or();
        qw.like(Posting::getContent,keyword);
        List<Posting> postings = dao.selectList(qw);
        List<VSimplePosting> vPostings = new ArrayList<>(postings.size());
        for (Posting posting: postings){
            vPostings.add(new VSimplePosting(posting));
        }
        return vPostings;
    }
}
